package com.example.myapplication.upload;

import android.content.Context;
import android.util.Log;

import com.chinanetcenter.wcs.android.api.FileUploader;
import com.chinanetcenter.wcs.android.api.ParamsConf;
import com.chinanetcenter.wcs.android.entity.OperationMessage;
import com.chinanetcenter.wcs.android.internal.UploadFileRequest;
import com.chinanetcenter.wcs.android.listener.FileUploaderListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

/**
 * @author: rongfeng
 * @date: 2020/10/29
 * @description 网宿云上传工具
 */
public class WSUploadUtil {

    public static final String PUT_URL = "https://bobei.up28.v1.wcsapi.com";
    //todo
    public static final String UPLOADER_TOKEN = "VEYSa5sKn0Ostwjzy4wFNH5IgxukwSU8PRWE:NTY1YTljOGVmMTM1YzgwNGU0MmQ2YjNhN2E3YjgzMzI5YmM4Nzg2Ng==:eyJzY29wZSI6InNob3J0dmlkZW8tdGVzdCIsImRlYWRsaW5lIjoiMTYzNTQ4ODI4MDAwMCIsInJldHVybkJvZHkiOiJmbmFtZT0kKGZuYW1lKSZ1cmw9JCh1cmwpIiwib3ZlcndyaXRlIjowLCJmc2l6ZUxpbWl0IjowLCJzZXBhcmF0ZSI6MH0=";

    public static final String TAG = WSUploadUtil.class.getSimpleName();


    public WSUploadUtil() {

    }

    //上传动态图片
    public void uploadDynamicPicture(Context context, File srcFile) {
        String cosPath = "social_dynamic/img/" + createFileName(srcFile);
        FileUploader.setUploadUrl(PUT_URL);
        uploadFile(context,srcFile, UPLOADER_TOKEN, cosPath, "image/jpeg");
    }

    //上传动态视频
    public void uploadDynamicVideo(Context context,File srcFile) {
        String cosPath = "social_dynamic/video/" + createFileName(srcFile);
        FileUploader.setUploadUrl(PUT_URL);
        uploadFile(context,srcFile, UPLOADER_TOKEN, cosPath, "video/mp4");
    }


    //取消上传
    public void cancelUpload(Context context){
        FileUploader.cancelRequests(context);
    }

    public String createFileName(File srcFile) {
        String fileName = srcFile.getName();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
//        String keyName = UserUtils.getUserInfo().userId.toString() + "_" + System.currentTimeMillis() / 1000 + suffix;//用户 id_时间戳
        //todo
        String keyName = System.currentTimeMillis() / 1000 + suffix;//用户 id_时间戳;
        return keyName;
    }



    public void uploadFile(Context context,File srcFile, String token, String fileDir, String mimeType) {
        Log.d(TAG, "canRead : " + srcFile.canRead());
        ParamsConf conf = new ParamsConf();
        // 原始文件名称
        conf.fileName = srcFile.getName();
        // 通过表单参数设置文件保存到云存储的名称
        conf.keyName = fileDir;
        // 通过表单参数设置文件的mimeType
        conf.mimeType = mimeType;
        FileUploader.setParams(conf);

        FileUploader.upload(context, token, srcFile, null, new FileUploaderListener() {
            @Override
            public void onSuccess(int status, JSONObject responseJson) {
                try {
                    String url = responseJson.getString("url");
                    Log.d(TAG, "url : " + url);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(OperationMessage operationMessage) {
                Log.e(TAG, "errorMessage : " + operationMessage.getMessage());
            }

            @Override
            public void onProgress(UploadFileRequest request, long currentSize, long totalSize) {
                String percent = ((float) currentSize / totalSize * 100) + "%";
                String progressMsg = "当前: " + currentSize + ", 总: " + totalSize +
                        ", 比例: " + percent + "\r\n";
                Log.d(TAG, progressMsg);
            }
        });

    }

}
